class ConDestructibleObjective extends ConObjective;

var(ConProperties) repnotify StaticMeshComponent LiveMesh, DeadMesh;
var repnotify int ConDestructionStage;

replication
{
	if (Role == ROLE_Authority)
		ConDestructionStage;
}

simulated event PostBeginPlay()
{
	Reset();
	Super.PostBeginPlay();
}

function bool Shootable()
{
	return true;
}

simulated function Reset()
{
	ConDestructionStage = 0;
	LiveMesh.SetHidden(False);
	DeadMesh.SetHidden(True);
	SetCollision(true, true);
	
	Super.Reset();
}

simulated function ConDestroy()
{
	ConDestructionStage = 1;
	Health = 0;
	LiveMesh.SetHidden(True);
	DeadMesh.SetHidden(False);
	SetCollision(false, false);
	bConComplete = true;
	ConqueredCastles(WorldInfo.Game).ObjectiveComplete(self);
}

simulated function CheckHealth()
{
	if (bConComplete == false)
	{
		if(health <= 0) ConDestroy();
	}
}

simulated event TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{		
	if (Damage <= 0 || WorldInfo.Game.bGameEnded || UTGame(WorldInfo.Game).ResetCountdown > 0 || InstigatedBy.PlayerReplicationInfo.Team.TeamIndex == ConOwnerTeam)
		return;
		
    if(health > 0) Health -= Damage;
    if (bConComplete == false)
	{
		if(health <= 0) ConDestroy();
	}

    bForceNetUpdate = true;
}

simulated event PostRenderFor(PlayerController PC, Canvas Canvas, vector CameraPosition, vector CameraDir)
{
        local float XL, HealthX;
        local vector ScreenLoc;

        ScreenLoc = Canvas.Project(Location) ;

    // make sure not clipped out
        if (screenLoc.X < 0 ||
                screenLoc.X >= Canvas.ClipX ||
                screenLoc.Y < 0 ||
                screenLoc.Y >= Canvas.ClipY)
        {
                return;
        }

       healthX = health*100/BaseHealth;
       XL = 100 ;
           class'UTHUD'.static.DrawHealth(ScreenLoc.X-0.5*XL, ScreenLoc.Y - 50, HealthX, XL, 20, Canvas);
}

// Kyle: Begin AI Controls

function bool TellBotHowToDisable(UTBot B)
{
	`log("Con: ConDestructibleObjective - TellBotHowToDisable");
	if (ConOwnerTeam == B.Squad.Team.TeamIndex)
		return false;
		
	if (B.CanAttack(self))
		return false;
	else
		return B.Squad.FindPathToObjective(B, self);

	if ( !B.Pawn.bStationary && B.Pawn.TooCloseToAttack(self) )
	{
		B.GoalString = "Back off from objective";
		B.RouteGoal = B.FindRandomDest();
		B.MoveTarget = B.RouteCache[0];
		B.SetAttractionState();
		return true;
	}
	else if ( B.CanAttack(self) )
	{
		if (KillEnemyFirst(B))
			return false;

		if ( !B.bTargetAlternateLoc || (UTPawn(B.Pawn) == None) )
		{
			B.GoalString = "Attack Objective";
			B.DoRangedAttackOn(self);
			return true;
		}
		B.GoalString = "Charge Objective";
		B.FireWeaponAt(self);
	}
	MarkShootSpotsFor(B.Pawn);
}

// Kyle: End AI Controls

defaultproperties
{
   RemoteRole=ROLE_SimulatedProxy
   ConDestructionStage=0
   BaseHealth=500
   CollisionType=COLLIDE_BlockAll
   BlockRigidBody=true
   bStatic=False
   bNoDelete=True
   bDestinationOnly=false
   bAlwaysRelevant=true

   NetUpdateFrequency=1
   Name="ConDestructibleObjective"
   ConObjectiveType=CO_Dest
}